/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.gui.sales;

import com.kimstore.bean.Customer;
import com.kimstore.bean.Item;
import com.kimstore.bean.ItemStatus;
import com.kimstore.bean.Order;
import com.kimstore.bean.Staff;
import com.kimstore.bean.User;
import com.kimstore.factory.DaoFactory;
import com.kimstore.gui.base.BaseWindow;
import com.kimstore.gui.main.CredentialsJOptionPane;
import com.kimstore.utilities.DateUtil;
import com.kimstore.utilities.Session;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.decorator.HighlighterFactory;

/**
 *
 * @author Administrator
 */
public class ViewUpdateOrderWindow extends BaseWindow {

    private ViewUpdateOrderController controller = null;

    /**
     * Creates new form ViewUpdateOrder
     */
    public ViewUpdateOrderWindow(ViewUpdateOrderController controller) {
        initComponents();

        this.controller = controller;

        prepareUserInterface();
    }

    public void populateCustomers(List<Customer> customers) {

        DefaultComboBoxModel model = (DefaultComboBoxModel) customerComboBox.getModel();

        model.addElement("");
        for (Customer c : customers) {
            model.addElement(c);
        }

    }

    public void populateStaffs(List<Staff> staffs) {
        DefaultComboBoxModel model = (DefaultComboBoxModel) staffComboBox.getModel();

        for (Staff s : staffs) {
            model.addElement(s);
        }
    }

    public void populateWindow(Order order) {
        if (order.getStaff() != null) {
            for(int i = 0; i < staffComboBox.getItemCount(); i++) {
                if(staffComboBox.getModel().getElementAt(i).toString().equals(order.getStaff().getName())) {
                    staffComboBox.setSelectedIndex(i);
                }
            }
        }

        if (order.getCustomer() != null) {
            customerComboBox.setSelectedItem(order.getCustomer());
        }

        for (Item i : order.getItems()) {
            DefaultTableModel model = (DefaultTableModel) itemsTable.getModel();

            ItemStatus mostRecentStatus = i.getHistory().get(i.getHistory().size() - 1);

            String priceSold = "Replaced";
            
            if(i.getPurchaseDetail() != null) {
                priceSold = i.getPurchaseDetail().getPriceSold() + "";
            }
            
            model.insertRow(model.getRowCount(), new Object[]{i.getSerial(), i.getProduct().getName(), priceSold, DateUtil.formatDate(mostRecentStatus.getDate()), mostRecentStatus.getType()});
        }

        if (order.getVoidedItems() != null) {
            DefaultTableModel model = (DefaultTableModel) voidedItemsTable.getModel();
            for (Item i : order.getVoidedItems()) {
                model.insertRow(model.getRowCount(), new Object[]{i.getSerial(), i.getProduct().getName()});
            }
        }

        customerTypeComboBox.setSelectedItem(order.getCustomerType());
    }

    public void refreshItemsTable(Order order) {
        DefaultTableModel model = (DefaultTableModel) itemsTable.getModel();
        model.setRowCount(0);

        for (Item i : order.getItems()) {
            ItemStatus mostRecentStatus = i.getHistory().get(i.getHistory().size() - 1);

            String priceSold = "Replaced";
            
            if(i.getPurchaseDetail() != null) {
                priceSold = i.getPurchaseDetail().getPriceSold() + "";
            }
            
            model.insertRow(model.getRowCount(), new Object[]{i.getSerial(), i.getProduct().getName(), priceSold, DateUtil.formatDate(mostRecentStatus.getDate()), mostRecentStatus.getType()});
        }
    }

    private void prepareUserInterface() {
        itemsTable.setShowGrid(true, true);
        itemsTable.addHighlighter(HighlighterFactory.createSimpleStriping());
        itemsTable.getTableHeader().setReorderingAllowed(false);
    }

    private void addItemDeliveryHistory(String status) {
        ArrayList<String> skus = new ArrayList<String>();
        int[] selectedRows = itemsTable.getSelectedRows();

        for (int i : selectedRows) {
            skus.add(itemsTable.getValueAt(i, 0).toString());
        }

        controller.addItemDeliveryHistoryStatus(skus, status, staffComboBox.getSelectedItem().toString());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        updateToggleButton = new javax.swing.JToggleButton();
        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        itemsTable = new org.jdesktop.swingx.JXTable();
        removeItemButton = new javax.swing.JButton();
        deliveredButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        voidedItemsTable = new org.jdesktop.swingx.JXTable();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        customerComboBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        staffComboBox = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        customerTypeComboBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("View Order");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("sansserif", 1, 14)); // NOI18N
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/kimstore/resources/images/document-blue-icon-medium.png"))); // NOI18N
        jLabel1.setText("View Order");

        updateToggleButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/kimstore/resources/images/pen-write-icon.png"))); // NOI18N
        updateToggleButton.setText("Update");
        updateToggleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateToggleButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Items"));

        itemsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "SKU", "Product", "Price Sold", "Last Update", "Status"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        itemsTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                itemsTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(itemsTable);

        removeItemButton.setText("Void  Item");
        removeItemButton.setEnabled(false);
        removeItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeItemButtonActionPerformed(evt);
            }
        });

        deliveredButton.setText("Delivered");
        deliveredButton.setEnabled(false);
        deliveredButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deliveredButtonActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("sansserif", 2, 12)); // NOI18N
        jLabel3.setText("Quick Update:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 663, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(removeItemButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deliveredButton)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                        .addComponent(deliveredButton))
                    .addComponent(removeItemButton))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Order Items", jPanel3);

        voidedItemsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "SKU", "Product Name"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(voidedItemsTable);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 663, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Voided Items", jPanel4);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 688, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/kimstore/resources/images/done.png"))); // NOI18N
        saveButton.setText("Save");
        saveButton.setEnabled(false);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/kimstore/resources/images/cancel.png"))); // NOI18N
        cancelButton.setText("Cancel");
        cancelButton.setEnabled(false);
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        jLabel2.setText("Customer");

        customerComboBox.setEditable(true);
        customerComboBox.setEnabled(false);

        jLabel4.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        jLabel4.setText("Delivery Staff");

        staffComboBox.setEnabled(false);

        jLabel5.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N
        jLabel5.setText("Type");

        customerTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Reseller", "Meet Ups", "Shipping", "Consignment", "Marketing" }));
        customerTypeComboBox.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(staffComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(customerTypeComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(customerComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(customerComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(staffComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(customerTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(updateToggleButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(updateToggleButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void updateToggleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateToggleButtonActionPerformed
        if (Session.getInstance().getUser().getType() != User.TYPE_ADMIN && Session.getInstance().getUser().getType() != User.TYPE_ADMIN_B) {
            CredentialsJOptionPane pane = new CredentialsJOptionPane(this);

            if (pane.showDialog() == JOptionPane.OK_OPTION) {
                String username = pane.getUsernameField().getText();
                String password = Arrays.toString(pane.getPasswordField().getPassword());

                User user = DaoFactory.getInstance().getUserDao().findByUsernamePassword(username, password);
                if (user != null && (user.getType() == User.TYPE_ADMIN || user.getType() == User.TYPE_ADMIN_B)) {
                    System.out.println("VALID!");
                    if (updateToggleButton.isSelected()) {
                        staffComboBox.setEnabled(true);
                        customerComboBox.setEnabled(true);
                        saveButton.setEnabled(true);
                        cancelButton.setEnabled(true);
                        removeItemButton.setEnabled(true);
                        deliveredButton.setEnabled(true);
                        customerTypeComboBox.setEnabled(true);

                    } else {
                        staffComboBox.setEnabled(false);
                        customerComboBox.setEnabled(false);
                        saveButton.setEnabled(false);
                        cancelButton.setEnabled(false);
                        removeItemButton.setEnabled(false);
                        deliveredButton.setEnabled(false);
                        customerTypeComboBox.setEnabled(false);
                    }
                } else {
                    //TO DO: poup
                    updateToggleButton.setSelected(false);
                }
            } else {
                updateToggleButton.setSelected(false);
            }
        } else {
            if (updateToggleButton.isSelected()) {
                staffComboBox.setEnabled(true);
                customerComboBox.setEnabled(true);
                saveButton.setEnabled(true);
                cancelButton.setEnabled(true);
                removeItemButton.setEnabled(true);
                deliveredButton.setEnabled(true);
                customerTypeComboBox.setEnabled(true);

            } else {
                staffComboBox.setEnabled(false);
                customerComboBox.setEnabled(false);
                saveButton.setEnabled(false);
                cancelButton.setEnabled(false);
                removeItemButton.setEnabled(false);
                deliveredButton.setEnabled(false);
                customerTypeComboBox.setEnabled(false);
            }
        }
    }//GEN-LAST:event_updateToggleButtonActionPerformed

    private void itemsTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_itemsTableMouseClicked
        if (evt.getClickCount() == 2) {
            String sku = itemsTable.getValueAt(itemsTable.getSelectedRow(), 0) + "";
            controller.createViewUpdateItemController(sku);
        }
    }//GEN-LAST:event_itemsTableMouseClicked

    private void deliveredButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deliveredButtonActionPerformed
        if (itemsTable.getSelectedRowCount() != 0) {
            addItemDeliveryHistory(ItemStatus.DELIVERED);
        }
    }//GEN-LAST:event_deliveredButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed


    private void removeItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeItemButtonActionPerformed
        if (itemsTable.getSelectedRowCount() == 1) {
            int row = itemsTable.getSelectedRow();
            String sku = itemsTable.getValueAt(row, 0).toString();
            //System.out.println(sku);
            controller.addToRemove(sku);

            Item item = controller.getItem(sku);
            ((DefaultTableModel) itemsTable.getModel()).removeRow(row);
            DefaultTableModel model = (DefaultTableModel) voidedItemsTable.getModel();
            model.insertRow(model.getRowCount(), new Object[]{item.getSerial(), item.getProduct().getName()});
        }
    }//GEN-LAST:event_removeItemButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        Staff staff = (Staff) staffComboBox.getSelectedItem();
        Customer customer;

        if (!customerComboBox.getSelectedItem().toString().trim().isEmpty()) {
            customer = DaoFactory.getInstance().getCustomerDao().findByName(customerComboBox.getSelectedItem().toString());

            if (customer == null) {
                customer = new Customer((String) customerComboBox.getSelectedItem());
            }
        } else {
            customer = null;
        }

        String customerType = customerTypeComboBox.getSelectedItem().toString();

        controller.updateOrder(staff, customer, customerType);
        this.dispose();
    }//GEN-LAST:event_saveButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JComboBox customerComboBox;
    private javax.swing.JComboBox customerTypeComboBox;
    private javax.swing.JButton deliveredButton;
    private org.jdesktop.swingx.JXTable itemsTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton removeItemButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JComboBox staffComboBox;
    private javax.swing.JToggleButton updateToggleButton;
    private org.jdesktop.swingx.JXTable voidedItemsTable;
    // End of variables declaration//GEN-END:variables
}
